//
//  MemberViewController.m
//  kularkDemo
//
//  Created by szblsx2 on 2020/4/18.
//  Copyright © 2020 szblsx. All rights reserved.
//

#import "MemberViewController.h"
#import "KLMemberListCell.h"
#import "KLSdk.h"

@interface MemberViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *muteButton1;
@property (weak, nonatomic) IBOutlet UIButton *muteButton2;

@end

@implementation MemberViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"管理成员";
    [self.tableView registerNib:[UINib nibWithNibName:@"KLMemberListCell" bundle:nil] forCellReuseIdentifier:kMemberListCellIdentifier];
    self.tableView.rowHeight = 80;
    self.muteButton1.layer.borderWidth = 1.0;
    self.muteButton1.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.muteButton1.layer.cornerRadius = 3.0;
    self.muteButton1.layer.masksToBounds = YES;
    self.muteButton2.layer.borderWidth = 1.0;
    self.muteButton2.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.muteButton2.layer.cornerRadius = 3.0;
    self.muteButton2.layer.masksToBounds = YES;

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

#pragma mark - Action

- (IBAction)muteButtonAction1:(UIButton *)sender {
    
    BOOL isManager = [self checkManager];
    if (!isManager) {
        [WHToast showMessage:@"没有权限"];
        return;
    }
//    if (self.meetConfig.roomMute) {
//        [WHToast showMessage:@"当前已经是静音状态"];
//        return;
//    }
    
    self.muteButton1.backgroundColor = [UIColor blueColor];
    self.muteButton2.backgroundColor = [UIColor clearColor];
//    [[KLSdk shared] roomMute:YES];
//    self.meetConfig.roomMute = YES;
    [WHToast showMessage:@"已开启全体静音"];
}

- (IBAction)muteButtonAction2:(UIButton *)sender {
    
    BOOL isManager = [self checkManager];
    if (!isManager) {
        [WHToast showMessage:@"没有权限"];
        return;
    }
//
//    if (!self.meetConfig.roomMute) {
//        [WHToast showMessage:@"当前已经是非静音状态"];
//        return;
//    }
    
    self.muteButton1.backgroundColor = [UIColor clearColor];
    self.muteButton2.backgroundColor = [UIColor blueColor];
//    [[KLSdk shared] roomMute:NO];
//    self.meetConfig.roomMute = NO;
    [WHToast showMessage:@"已解除全体静音"];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KLMemberListCell *cell = [tableView dequeueReusableCellWithIdentifier:kMemberListCellIdentifier forIndexPath:indexPath];
    cell.meetUser = self.dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  
//    BOOL isManager = [self checkManager];
//    if (!isManager) {
//        [WHToast showMessage:@"没有权限"];
//        return;
//    }
//
    KLMeetUser *meetUser = self.dataArray[indexPath.row];
    //先判断是否是自己
    if ([[KLUserInfo shared].userId isEqualToString:meetUser.userId]) {
        [WHToast showMessage:@"点击了自己"];
        return;
    }
    UIAlertController *alertController = [UIAlertController
                                          alertControllerWithTitle:nil
                                          message:nil
                                          preferredStyle: UIAlertControllerStyleActionSheet];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"设为主持人" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        [[KLSdk shared] presenterChange:meetUser.userId];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"移出会议" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self showAlertWithKickoutId:meetUser.userId];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    [alertController addAction:action1];
    [alertController addAction:action2];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)reload
{
    if (self.tableView) {
        [self.tableView reloadData];
    }
}

#pragma mark - PrivateMethod
- (void)showAlertWithKickoutId:(NSString *)userId
{
    UIAlertController *alertController = [UIAlertController
                                          alertControllerWithTitle:nil
                                          message:@"是否允许用户再次加入"
                                          preferredStyle: UIAlertControllerStyleActionSheet];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"不允许" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//       [[KLSdk shared] kickoutUser:userId disallow:YES];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"允许" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        [[KLSdk shared] kickoutUser:userId disallow:NO];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    [alertController addAction:action1];
    [alertController addAction:action2];
    [self presentViewController:alertController animated:YES completion:nil];
}

// 查询自己是否是主持人
- (BOOL)checkManager
{
    BOOL isManager = NO;
    for (KLMeetUser *user in self.dataArray) {
        if ([[KLUserInfo shared].userId isEqualToString:user.userId]) {
            if (user.isManager) {
                isManager = YES;
            }
            break;
        }
    }
    return isManager;
}

#pragma mark - Setter

- (NSArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}



@end
